package com.qf.lanzhou.shop.admin.service.impl;

import com.qf.lanzhou.shop.admin.dao.ChartMapper;
import com.qf.lanzhou.shop.admin.service.ChartService;
import com.qf.lanzhou.shop.pojo.entity.ChartPie;
import com.qf.lanzhou.shop.pojo.qo.CategorySumQo;
import com.qf.lanzhou.shop.pojo.qo.OrderDataQo;
import com.qf.lanzhou.shop.pojo.qo.SellerWeekAmountQo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author LEON-HE
 */
@Service
public class ChartServiceImpl implements ChartService {

    @Autowired
    private ChartMapper chartMapper;

    @Override
    public List<ChartPie> getPieData(Long id) {
        List<ChartPie> chartPies = new ArrayList<>();
        List<CategorySumQo> categorySumQos = chartMapper.selectPieBySellerId(id);
        Double sumNum = chartMapper.selectAllSaleVolBySellerId(id);
        categorySumQos.forEach(item->{
            ChartPie chartPie = new ChartPie();
            chartPie.setY(item.getPieSum()/sumNum);
            chartPie.setName(item.getPieCategory());
            chartPies.add(chartPie);
        });
        System.out.println(chartPies);
        return chartPies;
    }

    @Override
    public OrderDataQo getOrderData(Long id) {
        Integer san = chartMapper.selectSellerAmountNumBySellerId(id);
        Integer sgn = chartMapper.selectSellerGoodsNumBySellerId(id);
        Integer son = chartMapper.selectSellerOrderNumBySellerId(id);
        Integer ssn = chartMapper.selectSellerScanNumBySellerId(id);
        return new OrderDataQo(sgn,ssn,san,son);
    }

    @Override
    public List<BigDecimal> getWeekAmount(Long id) {
        // 获取时间与金额的list
        List<SellerWeekAmountQo> qos = chartMapper.selectSellerWeekAmountBySerllerId(id);
        System.out.println(qos);
        ArrayList<BigDecimal> decimals = new ArrayList<>();
        //获取当前时间
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        //对当前时间加减
        for (int i = 0; i <= 6; i++) {
            Calendar rightNow = Calendar.getInstance();
            rightNow.add(Calendar.DAY_OF_WEEK,-6+i);
            Date time = rightNow.getTime();
            String a = df.format(time);
            System.out.println(a);
            for (int j = 0; j < qos.size(); j++) {
                SellerWeekAmountQo qo = qos.get(j);
                if (qo.getUpdateTime().toString().equals(a)){
                    System.out.println(qo.getAmount());
                    decimals.add(qo.getAmount());
                    break;
                }
                if (j==qos.size()-1){
                    System.out.println(0);
                    decimals.add(new BigDecimal(0));
                }
            }
        }
        return decimals;
    }

}
